﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;

namespace ADNTimeline
{
    public class TimelineItem
    {
        private LayerItem layer;
        private FrameItem frame;
        private ItemFormat format = new ItemFormat();

        public TimelineItem(LayerItem layer, FrameItem frame, ItemFormat format)
        {
            this.layer = layer;
            this.frame = frame;
            if (format != null) { this.format = format; }
        }

        public TimelineItem(LayerItem layer, FrameItem frame)
        {
            this.layer = layer;
            this.frame = frame;
        }

        public LayerItem Layer
        {
            get { return layer; }
            set { layer = value; }
        }

        public FrameItem Frame
        {
            get { return frame; }
            set { frame = value; }
        }

        public ItemFormat Format
        {
            get { return format; }
            set { format = value; }
        }
    }

    public class ItemList : CollectionBase
    {
        public delegate void ItemListEventHandler(object sender, ItemListEventArgs e);
        public event ItemListEventHandler ItemAdded;
        public event ItemListEventHandler ItemRemoved;
        public event ItemListEventHandler ItemMoved;
        public event EventHandler ListCleared;

        private void OnItemAdded(int index, TimelineItem item)
        {
            if (ItemAdded != null) { ItemAdded(this, new ItemListEventArgs(index, item)); }
        }

        private void OnItemRemoved(int index, TimelineItem item)
        {
            if (ItemRemoved != null) { ItemRemoved(this, new ItemListEventArgs(index, item)); }
        }

        private void OnItemMoved(int currentIndex, int previousIndex, TimelineItem item)
        {
            if (ItemMoved != null) { ItemMoved(this, new ItemListEventArgs(currentIndex, previousIndex, item)); }
        }

        private void OnListCleared()
        {
            if (ListCleared != null) { ListCleared(this, EventArgs.Empty); }
        }

        public TimelineItem this[int index]
        {
            get { return (TimelineItem)List[index]; }
            set { List[index] = value; }
        }

        public int IndexOf(TimelineItem value)
        {
            return List.IndexOf(value);
        }

        public int Add(TimelineItem value)
        {
            return List.Add(value);
        }

        public void Insert(int index, TimelineItem value)
        {
            List.Insert(index, value);
        }

        public void Remove(TimelineItem value)
        {
            List.Remove(value);
        }

        [Description("Moves the item at the specified index up one. The object above is moved down")]
        public void MoveUp(int index)
        {
            if (List.Count > 0 && index > 0 && index <= List.Count - 1)
            {
                TimelineItem source = (TimelineItem)List[index];
                TimelineItem target = (TimelineItem)List[index - 1];

                List[index] = target;
                List[index - 1] = source;
                
                OnItemMoved(index - 1, index, (TimelineItem)List[index]);
            }
        }

        [Description("Moves the item at the specified index down one. The object above is moved up")]
        public void MoveDown(int index)
        {
            if (List.Count > 0 && index >= 0 && index < List.Count - 1)
            {
                TimelineItem source = (TimelineItem)List[index];
                TimelineItem target = (TimelineItem)List[index + 1];

                List[index] = target;
                List[index + 1] = source;

                OnItemMoved(index + 1, index, (TimelineItem)List[index]);
            }
        }

        protected override void OnInsertComplete(int index, object value)
        {
            base.OnInsertComplete(index, value);
            OnItemAdded(index, (TimelineItem)value);
        }

        protected override void OnClearComplete()
        {
            base.OnClearComplete();
            OnListCleared();
        }

        protected override void OnRemoveComplete(int index, object value)
        {
            base.OnRemoveComplete(index, value);
            OnItemRemoved(index, (TimelineItem)value);
        }


    }

    public class ItemListEventArgs : EventArgs
    {
        public ItemListEventArgs(int index, TimelineItem item)
        {
            this.CurrentIndex = index;
            this.Item = item;
        }

        public ItemListEventArgs(int currentIndex, int previousIndex, TimelineItem item)
        {
            this.CurrentIndex = currentIndex;
            this.PreviousIndex = previousIndex;
            this.Item = item;
        }

        public int CurrentIndex { get; set; }
        public int PreviousIndex { get; set; }
        public TimelineItem Item { get; set; }
    }

    public class ItemFormat
    {
        private int itemHeight = 20;
        private Color itemBackColor = Color.White;
        private Color itemSelectedBackColor = Color.LightBlue;

        public int ItemHeight
        {
            get { return itemHeight; }
            set { itemHeight = value; }
        }

        public Color ItemBackColor
        {
            get { return itemBackColor; }
            set { itemBackColor = value; }
        }

        public Color ItemSelectedBackColor
        {
            get { return itemSelectedBackColor; }
            set { itemSelectedBackColor = value; }
        }
    }
}
